﻿import mx.utils.Delegate;

import kliment.display.MCC;
import kliment.controls.ControlsAbstract;
/**
 * ...
 * @author	Kliment
 * @version	1.2.0
 */
class kliment.controls.AutoHide extends ControlsAbstract {
	private var _hitSpace_mc:MovieClip;
	private var _delay:Number;
	private var _mouseOver:Boolean = true;
	private var _hideIntervalID:Number;
	
	public var showFunction:Function;
	public var hideFunction:Function;
	
	public function AutoHide(hitSpace_mc:MovieClip, delay:Number) {
		_hitSpace_mc = hitSpace_mc;
		_delay = delay;
		
		enable = _enable;
	}
	
	public function get auto():Boolean { return _enable };
	public function set auto(value:Boolean):Void {
		enable = value;
	}
	
	public function set enable(value:Boolean):Void {
		if (value) {
			_hitSpace_mc.onEnterFrame = Delegate.create(this, _hitSpace);
		} else {
			_hitSpace_mc.onEnterFrame = null;
			clearInterval(_hideIntervalID);
			show();
		}
		super.enable = value;
	}
	
	private function _hitSpace():Void {
		var hitTest:Boolean = MCC.hitTestMouse(_hitSpace_mc);
		if (hitTest && !_mouseOver) {
			show();
			_mouseOver = true;
		} else if (!hitTest && _mouseOver) {
			_hide();
			_mouseOver = false;
		}
	}
	
	public function show():Void {
		clearInterval(_hideIntervalID);
		showFunction();
	}
	
	private function _hide():Void {
		clearInterval(_hideIntervalID);
		_hideIntervalID = setInterval(this, 'hide', _delay);
	}
	
	public function hide():Void {
		clearInterval(_hideIntervalID);
		hideFunction();
	}
	
	public function remove():Void {
		_hitSpace_mc.onEnterFrame = null;
		clearInterval(_hideIntervalID);
		delete this;
	}
}